package Darstellung.Objekte;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import Spiel.Objekte.IObjekt;
import Spiel.Objekte.Objekt;
import Spiel.Objekte.Tasche;

public class JTasche extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Tasche tasche;

	public JTasche(Tasche tasche) {
		this.tasche = tasche;
		this.setLayout(null);
		this.setSize(180, 200);
		this.setLocation(200, 160);
		this.setBackground(Color.GRAY);
		this.setVisible(false);

		rahmen();
		anzeige();
		objekte();
	}

	private void rahmen() {
		JPanel p_0 = new JPanel();
		this.add(p_0);
		p_0.setSize(this.getWidth() - 20, 10);
		p_0.setLocation(10, 0);
		p_0.setBackground(Color.RED);

		JPanel p_1 = new JPanel();
		this.add(p_1);
		p_1.setSize(10, this.getHeight() - 20);
		p_1.setLocation(this.getWidth() - 10, 10);
		p_1.setBackground(Color.RED);

		JPanel p_2 = new JPanel();
		this.add(p_2);
		p_2.setSize(this.getWidth() - 20, 10);
		p_2.setLocation(10, this.getHeight() - 10);
		p_2.setBackground(Color.RED);

		JPanel p_3 = new JPanel();
		this.add(p_3);
		p_3.setSize(10, this.getHeight() - 20);
		p_3.setLocation(0, 10);
		p_3.setBackground(Color.RED);
	}

	private JTextArea ta;

	private void anzeige() {
		ta = new JTextArea();
		this.add(ta);
		ta.setSize(140, 130);
		ta.setLocation(20, 30 + 20);
		ta.setBackground(new Color(0, 0, 0, 0));
		ta.setEditable(false);
		ta.setFocusable(false);
		inhaltAktuallisieren();
	}

	public void inhaltAktuallisieren() {
		int max = 22;
		int counter = 0;
		ta.setText("Tasche:" + "\n");
		for (IObjekt objekt : tasche.getObjekte()) {
			String text = objekt.getName();
			ta.setText(ta.getText() + text);
			counter += text.length();
			if (counter >= max) {
				counter = 0;
				ta.setText(ta.getText() + "\n");
			} else {
				ta.setText(ta.getText() + ",");
				counter += 1;
			}
		}
	}

	private void objekte() {
		JButton b = new JButton("Add Objekt");
		this.add(b);
		b.setSize(70, 30);
		b.setLocation(20, 20);
		b.setBackground(new Color(200, 100, 200, 150));

		b.setFocusable(false);
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				int size = (int) ((Math.random() * 100) % 5) + 3;
				String name = "";
				name += (char) ((int) (Math.random() * 23) + 'A');
				for (int i = 1; i < size; i++) {
					name += (char) ((int) (Math.random() * 23) + 'a');
				}
				tasche.addObjekt(new Objekt("" + name));
				inhaltAktuallisieren();
			}
		});

		b = new JButton("Clear");
		this.add(b);
		b.setSize(70, 30);
		b.setLocation(20 + 70, 20);
		b.setBackground(new Color(200, 0, 0, 150));

		b.setFocusable(false);
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				tasche.removeAllObjekt();
				inhaltAktuallisieren();
			}
		});
	}

}
